package com.hna.eking.AirlineServer.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hna.eking.AirlineServer.Exception.RunServiceException;
import com.hna.eking.AirlineServer.Mapper.AirportInfo;
import com.hna.eking.AirlineServer.Mapper.AirportInfoMapper;

/**
 *
 * 机场表的控制类 Date : 2018-9-11 Description: 机场控制维护的类 Copyright: Copyright &copy;
 * 2018 Company :aivac
 * 
 * @author APP BD lele
 * @version V1.0 修改历史 序号 日期 修改人 修改原因 1
 * 
 */

@Service
public class AirportInfoService {
	@Autowired
	private AirportInfoMapper airportmapper;
	@Autowired
	private MapInfoService mapinfosvr;
	private Logger logger = LoggerFactory.getLogger(AirportInfoService.class);
	@Value("${visualcharts.state.enable}")
	private String ROW_STATUS_ENABLE;
	/*
	 * 查询指定页航图信息 
	 * 	参数： 机场代码：airportcode 
	 * 	省：province 模糊匹配 
	 * 	机场名：airportname 模糊匹配
	 * 	返回条数：limit 
	 * 	跳过条数：offset 
	 * 返回： 航图信息列表 {count：查询总条数，maplist:[航图列表]}
	 *
	 */
	public Map<String, Object> SelectListByInput(String airportcode, String province, String airportname, int limit, int offset)
			throws RunServiceException {
		//List<Map<String, Object>>  ret = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {		
			/* 查询所有记录条数 */
			int Count = airportmapper.getCountByInput(airportcode, province, airportname);
			if (Count == 0) {
				logger.warn("select airport info row is 0");
				throw new RunServiceException(RunServiceException.DATA_NOT_FOUND);
			}

			/* 查询机场信息 */
			List<AirportInfo> list = airportmapper.selectListByInput(airportcode, province, airportname, limit,
					offset);
			if (list.size() == 0) {
				logger.warn("select airport info row is 0");
				throw new RunServiceException(RunServiceException.DATA_NOT_FOUND);
			}
			logger.debug("mapinfolist:"  + Count);
			ObjectMapper mapper = new ObjectMapper();

			map.put("count", Count);
			map.put("airportlist", list);
			
		}catch (DataAccessException e) {
			logger.error("DATABASE ERROR");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		} 

		return map;
	}
	
	/*
	 * 插入指机场信息信息 
	 * 参数： 航图信息：AirportInfo
	 * 
	 * 返回： 无
	 *
	 */
	@Transactional(rollbackFor = RunServiceException.class)
	public void AirportInsert(AirportInfo airportinfo) throws RunServiceException {
		try {
			Date SysDate = new Date();
			String User = airportinfo.getCreateUser();
			/* 创建用户不能为空，否则失败 */
			if (User == null || "".equals(User)) {
				logger.error("create user can not be null");
				throw new RunServiceException(RunServiceException.UNKONW_USER);
			}
			//airportinfo.setMapVersion(SysDate);
			airportinfo.setCreateDate(SysDate);
			airportinfo.setUpdateDate(SysDate);
			airportinfo.setUpdateUser(User);
			//airportinfo.setAirportVersion(SysDate);
			//airportinfo.setMapVersion(SysDate);
			airportinfo.setEffectFlag(ROW_STATUS_ENABLE);
			Map<String, Object> map = new HashMap<String, Object>();
			String ret = null;

			/*查询最大机场版本*/
			int maxVersion = airportmapper.selectMaxAirportVersion();
			/* 插入信息 */
			airportinfo.setAirportVersion(maxVersion + 1);
			airportmapper.InsertAirportInfo(airportinfo);

		} catch(DuplicateKeyException ex) {
			logger.error("unique error");
			ex.printStackTrace();
			throw new RunServiceException(RunServiceException.UNIQUE_ERROR);
		}catch (DataAccessException e) {
		
			logger.error("database error");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		}
	}
	
	/*
	 * 更新指机场信息信息 
	 * 参数： 航图信息：AirportInfo
	 * 
	 * 返回： 无
	 *
	 */
	@Transactional(rollbackFor = RunServiceException.class)
	public void AirportUpdate(AirportInfo airportinfo) throws RunServiceException {
		try {
			Date SysDate = new Date();
			String User = airportinfo.getUpdateUser();
			/* 更新用户不能为空，否则失败 */
			if (User == null || "".equals(User)) {
				logger.error("create user can not be null");
				throw new RunServiceException(RunServiceException.UNKONW_USER);
			}
			
			airportinfo.setUpdateDate(SysDate);
			airportinfo.setUpdateUser(User);
			//airportinfo.setAirportVersion(SysDate);
			//airportinfo.setMapVersion(SysDate);
			airportinfo.setEffectFlag(ROW_STATUS_ENABLE);
			Map<String, Object> map = new HashMap<String, Object>();
			String ret = null;

			

			/* 更新信息 */
			airportmapper.updateAirportByKey(airportinfo);

		} catch(DuplicateKeyException ex) {
			logger.error("unique error");
			ex.printStackTrace();
			throw new RunServiceException(RunServiceException.UNIQUE_ERROR);
		}catch (DataAccessException e) {
		
			logger.error("database error");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		}
	}
	
	/*
	 * 逻辑删除指定机场 
	 * 参数： 
	 * 机场代码：airportcode 
	 * 
	 * 返回： 无
	 *
	 */
	@Transactional(rollbackFor = RunServiceException.class)
	public void DeleteAirportInfo(String airportcode, String User) throws RunServiceException {
		try {
			Date SysDate = new Date();
			
			
			/* 更新用户不能为空，否则失败 */
			if (User == null || "".equals(User)) {
				logger.error("create user can not be null");
				throw new RunServiceException(RunServiceException.UNKONW_USER);
			}
			/*删除机场下所有航图*/
			mapinfosvr.DeleteAllMapInfo(airportcode, User);

			/* 删除记录 */
			airportmapper.DeleteAirportInfo(airportcode, User, SysDate);

			
		} catch (DataAccessException e) {
			logger.error("database errorl");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		}
	}
	/*
	 * 查询所有机场代码和名称 
	 * 参数： 
	 * all
	 * 
	 * 返回：{"airportcode":"","airportname":""}
	 * 	 *
	 */
	public List<Map> SelectAllEft(){
		String Ret = null;
		//JsonNodeFactory factory = new JsonNodeFactory(false);
        //JsonFactory jsonFactory = new JsonFactory();
       List<Map> Item = new ArrayList<Map>();
        
		//JsonObject json = new 
       try {
		List<AirportInfo> list = airportmapper.selectAllEfft();
		for(int i = 0; i < list.size(); i++) {
			 Map map = new HashMap();
			 map.put("airportcode", list.get(i).getAirportCode());
			 map.put("airportname", list.get(i).getAirportName());
			 Item.add(map);

		}
		//ObjectNode root = factory.objectNode();
		//root.set("", Item);
		return Item;
       }catch (DataAccessException e) {
			logger.error("DATABASE ERROR");
			e.printStackTrace();
			throw new RunServiceException(RunServiceException.DATABASE_ERROR);
		} 
	}
}
